package com.cyan.app.workflow.activity.LGS;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cyan.app.barcode.CaptureActivity;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseActivity;
import com.cyan.app.workflow.bean.CommonBean;
import com.cyan.app.workflow.bean.DelPoolListBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.customview.ShowTipDialog;
import com.cyan.app.workflow.util.AppUtils;
import com.google.gson.Gson;

import org.json.JSONObject;
import com.cyan.app.workflow.util.network.GetRequestParams;

/**
 * Created by DXW on 2017-07-18.
 */

public class DellzxActivity extends BaseActivity {
    private TextView mTitleName;
    private Button mTitleSeach;
    private View mTitleView;
    private EditText mEditTextUser;
    private EditText mEditTextWahqy;
    private EditText mEditTextMaxqy;
    private EditText mEditTextAdqy;
    private EditText mEditTextBadqy;
    private Button mButtonUser;
    private Button mButtonSubmit;
    private EditText mEditTextManno;
    private EditText mEditTextNextpross;

    private DelPoolListBean.DelpoolBean delpoolBean;
    private static final String USERNO = "DEL_LZX_USERNO";
    private ShowTipDialog showdialog = new ShowTipDialog();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lgs_del_lzx);
        delpoolBean = (DelPoolListBean.DelpoolBean) getIntent().getSerializableExtra("delpoolDetail");
        titleBar();
        initView();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0x0001 && resultCode == RESULT_OK) {
            String result = "";
            if(data != null) {
                result = data.getStringExtra("result");
            }
            if (!result.contains("40010")) {
                AppUtils.showTip(DellzxActivity.this, "员工不存在");
            }else {
                String pisUser =  result.substring(5);
                mEditTextUser.setText(pisUser);
                PreferenceManager.getDefaultSharedPreferences(this).edit().putString(USERNO,pisUser).commit();
            }
        }
        if(requestCode == 0x0003 && resultCode == RESULT_OK) {
            Intent intent = new Intent();
            intent.putExtra("poolno",delpoolBean.getPoolno());
            DellzxActivity.this.setResult(RESULT_OK,intent);
            finish();
        }
    }
    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //MobclickAgent.onPause(this);
        //MobclickAgent.onPageEnd("DDActivity");
    }

    @Override
    protected void onResume() {
        super.onResume();
        //MobclickAgent.onResume(this);
        //MobclickAgent.onPageStart("DDActivity");
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //DDDataFetcher.getInstance().unRegistPoolsObserver(this);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    private void titleBar() {
        mTitleName = (TextView) findViewById(R.id.top_title_info);
        mTitleName.setText("流转下工序");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
        if(delpoolBean.getIsqc()==1) {
            mTitleSeach = (Button) findViewById(R.id.top_title_refres_with_icon);
            mTitleSeach.setVisibility(View.INVISIBLE);
            mTitleSeach = (Button) findViewById(R.id.top_title_refres);
            mTitleSeach.setVisibility(View.VISIBLE);
            mTitleSeach.setText("再送检");
            mTitleSeach.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("delpoolDetail", delpoolBean);
                    Intent intent = new Intent();
                    intent.setClass(DellzxActivity.this, DellzsActivity.class);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, 0x0003);
                }
            });
        }
        mTitleView = (View) findViewById(R.id.layout_main_ui_top2_title);
    }

    private void initView() {
        View resultView =  findViewById(R.id.lgs_del_lzx_result_lay);
        mEditTextMaxqy = (EditText)findViewById(R.id.lgs_del_lzx_maxqy);
        if(delpoolBean.getIsqc()==1) {
            TextView mEditTextQc = (TextView) findViewById(R.id.lgs_del_lzx_chqy);
            mEditTextQc.setText("送检 " + delpoolBean.getAccqty().toString());

            TextView mEditTextOk = (TextView) findViewById(R.id.lgs_del_lzx_okqy);
            mEditTextOk.setText("良品 " +  delpoolBean.getOkqty());

            TextView mEditTextNg = (TextView) findViewById(R.id.lgs_del_lzx_ngqy);
            mEditTextNg.setText("不良 " + delpoolBean.getNgqty().toString());

            TextView mEditTextBad = (TextView) findViewById(R.id.lgs_del_lzx_badqy);
            mEditTextBad.setText("报废 " + delpoolBean.getBadqty().toString());

            TextView mEditTextBck = (TextView) findViewById(R.id.lgs_del_lzx_bckqy);
            mEditTextBck.setText("退回 " + delpoolBean.getBckqy1().toString());
        }else {
            resultView.setVisibility(View.GONE);
            delpoolBean.setOkqty(delpoolBean.getAccqy1());
        }
        mEditTextWahqy = (EditText)findViewById(R.id.lgs_del_lzx_wahqy);
        mEditTextWahqy.setInputType(InputType.TYPE_NULL);
        mEditTextWahqy.setText(delpoolBean.getWahqy().toString());
        mEditTextMaxqy.setText(String.valueOf(delpoolBean.getChqy1()-delpoolBean.getBckqy1()-delpoolBean.getWahqy()));
        mEditTextAdqy = (EditText)findViewById(R.id.lgs_del_lzx_adqy);
        mEditTextAdqy.setText("0");
        mEditTextBadqy = (EditText)findViewById(R.id.lgs_del_lzx_badqy1);
        mEditTextBadqy.setText("0");

        mEditTextManno = (EditText)findViewById(R.id.lgs_del_lzx_manno);
        mEditTextManno.setText(delpoolBean.getSourceno());
        mEditTextNextpross = (EditText)findViewById(R.id.lgs_del_lzx_nextpross);
        mEditTextNextpross.setText(String.valueOf(delpoolBean.getFprossname()));

        mEditTextUser=(EditText)findViewById(R.id.lgs_del_lzx_user);
        mEditTextUser.setInputType(InputType.TYPE_NULL);
        String ygbh = PreferenceManager.getDefaultSharedPreferences(this).getString(USERNO,null);
        if(ygbh!=null){
            mEditTextUser.setText(String.valueOf(ygbh));
        }
        mButtonUser =(Button)findViewById(R.id.lgs_del_lzx_user_sm);
        mButtonUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(DellzxActivity.this, CaptureActivity.class), 0x0001);
            }
        });
        mButtonSubmit = (Button)findViewById(R.id.lgs_del_lzx_submit);
        mButtonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //送检
                if (mEditTextMaxqy.getText().length() == 0||Double.parseDouble(mEditTextMaxqy.getText().toString())< 0) {
                    AppUtils.showTip(DellzxActivity.this, "良品数必须大于等于0");
                }else if (mEditTextBadqy.getText().length() == 0||Double.parseDouble(mEditTextBadqy.getText().toString())< 0) {
                    AppUtils.showTip(DellzxActivity.this, "报废数必须大于等于0");
                }else if (Double.parseDouble(mEditTextBadqy.getText().toString())>0&&delpoolBean.getBadqty()<= 0) {
                    AppUtils.showTip(DellzxActivity.this, "质检结果无报废，不能报废");
                }else if ((Double.parseDouble(mEditTextBadqy.getText().toString())+Double.parseDouble(mEditTextMaxqy.getText().toString()))<=0) {
                    AppUtils.showTip(DellzxActivity.this, "良品数+报废数之和必须大于0");
                }else if (Double.parseDouble(mEditTextMaxqy.getText().toString()) + Double.parseDouble(mEditTextBadqy.getText().toString())>delpoolBean.getChqy1()-delpoolBean.getBckqy1()-delpoolBean.getWahqy()) {
                    AppUtils.showTip(DellzxActivity.this, "流转数量不能大于待处理数，待处理数=点收数-已流转数-已退回数");
                } else if (mEditTextUser.getText().toString().isEmpty()) {
                    AppUtils.showTip(DellzxActivity.this, "送检人员不能为空");
                } else {
                    //mPopWindow.dismiss();
                    new submitAsyncLoader().execute(mEditTextMaxqy.getText().toString(),mEditTextAdqy.getText().toString(),mEditTextUser.getText().toString(),mEditTextBadqy.getText().toString());
                }
            }
        });
    }

    public class submitAsyncLoader extends AsyncTask<String, Integer, String> {
        @Override
        protected void onPreExecute() {
            showdialog.showDialog(DellzxActivity.this);
        }

        @Override
        protected String doInBackground(String... params) {
            String result = null;
            String action ="511";
            try{
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("poolno", delpoolBean.getPoolno());
                jsonObject.put("wahqy",params[0]);
                jsonObject.put("adqy",params[1]);
                jsonObject.put("userno", params[2]);
                jsonObject.put("badqy", params[3]);
                result= GetRequestParams.getRequestResult(HttpConfig.HTTP_MAIN_SERVER_URL, action, jsonObject.toString(), getApplicationContext());
            }catch (Exception e){
                Log.e("error", e.toString());
            }
            return result;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s == null||s.isEmpty()||s.contains("html")) {
                AppUtils.showTip(DellzxActivity.this,"操作异常，请稍后再试！");
                showdialog.closeDialog();
                return;
            }
            CommonBean commonBean = new Gson().fromJson(s, CommonBean.class);
            if (commonBean.getCode()==1) {
                Intent intent = new Intent();
                intent.putExtra("poolno",delpoolBean.getPoolno());
                DellzxActivity.this.setResult(RESULT_OK,intent);
                AppUtils.showTip(DellzxActivity.this,"保存成功。");
                showdialog.closeDialog();
                finish();
            }else {
                DellzxActivity.this.setResult(RESULT_CANCELED);
                AppUtils.showTip(DellzxActivity.this,"保存失败，"+commonBean.getMsg());
                showdialog.closeDialog();
            }
        }

        @Override
        protected void onCancelled(String s) {
            DellzxActivity.this.setResult(RESULT_CANCELED);
        }
    }
}
